package com.wwwarehouse.carddesk.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.bean.CardDeskGroupGetGroupBean;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.ScreenUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GroupDetailsAdapter extends BaseAdapter {
    private Context mContext;
    private List<CardDeskGroupGetGroupBean.ListBean> mCardDeskFunctionList;
    private int mItemHeight;
    private int mItemWidth;
    private final int isLogined = 1, isLoginNo = 0;
    private static final String CREATOR = "creator", REGULATOR = "regulator", SUPERVISO = "supervisor", UP = "up", DOWN = "down";
    private Map<String, String> map;

    public GroupDetailsAdapter(Context context, List<CardDeskGroupGetGroupBean.ListBean> cardDeskFunctionList) {
        this.mContext = context;
        this.mCardDeskFunctionList = cardDeskFunctionList;
        caculateItemHeight();
        initRoleMap();
    }

    private void caculateItemHeight() {
        int screenHeight = ScreenUtils.getScreenHeight(mContext);
        int screenWidth = ScreenUtils.getScreenWidth(mContext);
        int barHeight = ScreenUtils.getStatusHeight(mContext);
        mItemHeight = (screenHeight - barHeight - ConvertUtils.dip2px(mContext, 120)) / 3;
    }

    @Override
    public int getCount() {
        return mCardDeskFunctionList == null ? 0 : mCardDeskFunctionList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCardDeskFunctionList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final CardDeskGroupGetGroupBean.ListBean infoCardsBean = mCardDeskFunctionList.get(position);
        final ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_group_details, null);
            holder = new ViewHolder();
            holder.allLin = (LinearLayout) convertView.findViewById(R.id.desk_group_gridview_lin);
            holder.deskGroupCircle = (CircleImageView) convertView.findViewById(R.id.desk_group_circleimage);
            holder.twoCircleImage1 = (CircleImageView) convertView.findViewById(R.id.desk_group_circleimage_1);
            holder.twoCircleImage2 = (CircleImageView) convertView.findViewById(R.id.desk_group_circleimage_2);
            holder.loginTxt = (TextView) convertView.findViewById(R.id.desk_group_main_adapter_logintxt);
            holder.nameTxt = (TextView) convertView.findViewById(R.id.desk_group_main_adapter_nametxt);
            holder.rankBtn = (StateButton) convertView.findViewById(R.id.desk_group_main_adapter_rankingbtn);
            holder.actorTxt = (TextView) convertView.findViewById(R.id.desk_group_main_adapter_idtxt);
            holder.circleimageRel = (RelativeLayout) convertView.findViewById(R.id.desk_group_circleimage_rel);

            holder.delayBtn = (StateButton) convertView.findViewById(R.id.desk_group_main_delaybtn);
            holder.delayBtn.setRadius(new float[]{0, 0, 0, 0, ConvertUtils.dip2px(mContext, 3), ConvertUtils.dip2px(mContext, 3), ConvertUtils.dip2px(mContext, 3), ConvertUtils.dip2px(mContext, 3)});
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.height = mItemHeight;
            convertView.setLayoutParams(params);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.allLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (gridviewClicklistener != null) {
                    gridviewClicklistener.click(infoCardsBean);
                }
            }
        });


        //登录状态
        if (isLogined == infoCardsBean.getIsLogin()) {
            holder.loginTxt.setTextColor(mContext.getResources().getColor(R.color.common_color_c7_96999e));
            holder.loginTxt.setText("" + mContext.getString(R.string.teamdesk_group_offline));
            //非登录状态
        } else {
            holder.loginTxt.setTextColor(mContext.getResources().getColor(R.color.common_color_c1_337cff));
            holder.loginTxt.setText("" + mContext.getString(R.string.teamdesk_group_inline));
        }

        holder.nameTxt.setText(infoCardsBean.getPersonName());


        //是否有延时任务：0，1有
        if (infoCardsBean.getIsDelayed() == null || infoCardsBean.getIsDelayed() == 0) {
            holder.delayBtn.setVisibility(View.GONE);
        } else {
            holder.delayBtn.setVisibility(View.VISIBLE);

        }

        if (CREATOR.equals(infoCardsBean.getBuIdManagerType()) && CREATOR.equals(infoCardsBean.getCardManagerType())) {
            holder.rankBtn.setVisibility(View.GONE);
        } else {
            if (infoCardsBean.getRanking() == null || infoCardsBean.getRanking() == 0) {
                holder.rankBtn.setVisibility(View.GONE);
            } else {
                holder.rankBtn.setVisibility(View.VISIBLE);
                holder.rankBtn.setText("" + (mContext.getString(R.string.teamdesk_group_adapter_ranktxt)).replace("$", "" + infoCardsBean.getRanking()));
            }
        }

        if (UP.equals(infoCardsBean.getCardManagerType())) {
            holder.circleimageRel.setVisibility(View.VISIBLE);
            holder.deskGroupCircle.setVisibility(View.GONE);
            BaseApplication.getApplicationInstance().displayImg(infoCardsBean.getFaceUrl(), holder.twoCircleImage1);
            BaseApplication.getApplicationInstance().displayImg(BaseApplication.sp.getValue(Constant.sp_Face_Url), holder.twoCircleImage2);

        } else {
            holder.circleimageRel.setVisibility(View.GONE);
            holder.deskGroupCircle.setVisibility(View.VISIBLE);
            BaseApplication.getApplicationInstance().displayImg(infoCardsBean.getFaceUrl(), holder.deskGroupCircle);
        }

        holder.actorTxt.setText(getRoleName(infoCardsBean));


        return convertView;
    }

    private class ViewHolder {
        private CircleImageView deskGroupCircle;
        private StateButton delayBtn, rankBtn;
        private CircleImageView twoCircleImage1, twoCircleImage2;
        private LinearLayout allLin;
        private TextView loginTxt;
        private TextView nameTxt;
        private TextView actorTxt;
        private RelativeLayout circleimageRel;
    }

    //获取角色的名字
    public String getRoleName(CardDeskGroupGetGroupBean.ListBean infoCardsBean) {
        String name;
        //显示角色名
        switch (infoCardsBean.getCardManagerType()) {
            case UP:
                name = map.get(UP);
                break;
            case DOWN:
                name = map.get(DOWN);
                break;
            case CREATOR:
                name = map.get(CREATOR);
                break;
            case SUPERVISO:
                name = map.get(SUPERVISO);
                break;
            case REGULATOR:
                name = map.get(REGULATOR);
                break;
            default:
                name = "";
                break;
        }

        return name;
    }

    //初始化角色名字 映射
    public void initRoleMap() {
        map = new HashMap<>();
        map.put(UP, "" + mContext.getString(R.string.teamdesk_group_adapter_up));
        map.put(DOWN, "" + mContext.getString(R.string.teamdesk_group_adapter_down));
        map.put(CREATOR, "" + mContext.getString(R.string.teamdesk_group_adapter_creator));
        map.put(SUPERVISO, "" + mContext.getString(R.string.teamdesk_group_adapter_supervisor));
        map.put(REGULATOR, "" + mContext.getString(R.string.teamdesk_group_adapter_manager));
    }

    public void setGridviewClicklistener(GridviewClicklistener clicklistener) {
        this.gridviewClicklistener = clicklistener;
    }

    GridviewClicklistener gridviewClicklistener;

    public interface GridviewClicklistener {
        void click(CardDeskGroupGetGroupBean.ListBean bean);
    }
}